package com.bogu.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;


import com.bogu.constant.ConstantS;
import com.bogu.model.Album;
import com.bogu.model.Biology;
import com.bogu.model.Category;
import com.bogu.model.Collection;
import com.bogu.model.Comment;
import com.bogu.model.Course;
import com.bogu.model.Dynamic;
import com.bogu.model.Notice;

import com.bogu.model.Record;
import com.bogu.model.Share;
import com.bogu.model.Type;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

public class BiologyDao extends ResourceGetBase {
	private Context context;
	private Handler handler;
	private Gson gson = new Gson();
	private ProgressDialog pd;
	private ArrayList<Category> categories;

	public BiologyDao(Context context, Handler handler) {
		super(context, handler);
		this.context = context;
		this.handler = handler;
	}

	public void toastInfo(String str) {
		Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
	}

	public void getAlbum(int user_id) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("user_id", user_id + "");
		params.addBodyParameter("type", "get_album");
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
				
						
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
						
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("albums"),
									new TypeToken<ArrayList<Album>>() {
									}.getType());
							msg.what = 0;
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}
					}
				});
	}
	public void getAlbums(int user_id) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("user_id", user_id + "");
		params.addBodyParameter("type", "get_albums");
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 1;
						handler.sendMessage(msg);
						
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 2;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("albums"),
									new TypeToken<ArrayList<Album>>() {
									}.getType());
							msg.what = 0;
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}
					}
				});
	}
	public void getField() {

		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_field");
	
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						categories = (ArrayList<Category>) readObject("categories");
						if (categories != null) {
							Message message = new Message();
							message.obj = categories;
							message.what = 0;
							handler.sendMessage(message);// 发回成功消息
							return;
						}
						toastInfo("网络连接错误");

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							categories = gson.fromJson(
									jsonObject.getString("categories"),
									new TypeToken<ArrayList<Category>>() {
									}.getType());
							msg.obj = categories;
							msg.what = 0;
							handler.sendMessage(msg);
							new Thread() {
								@Override
								public void run() {
									saveObject(categories, "categories");
								}
							}.start();
						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

							msg.what = 3;
							handler.sendMessage(msg);
						}
					}
				});

	}
public void getMyCollection(int user_id,String method){
	HttpUtils httpUtils = new HttpUtils(6 * 1000);
	httpUtils.configRequestRetryCount(3);
	RequestParams params = new RequestParams();
	params.addBodyParameter("method",method);
	params.addBodyParameter("type", "get_my_collection");
	params.addBodyParameter("user_id",user_id+"");
	httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
			+ ConstantS.server + "BiologyServlet", params,
			new RequestCallBack<String>() {

				@Override
				public void onStart() {
					// TODO 自动生成的方法存根
					Message msg = new Message();
					msg.what = 9;
					handler.sendMessage(msg);
				}

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					// TODO Auto-generated method stub
					toastInfo("网络连接错误");
					Message msg = new Message();
					msg.what = 12;
					handler.sendMessage(msg);
				}

				@Override
				public void onSuccess(ResponseInfo<String> arg0) {
					// TODO Auto-generated method stub

					JSONObject jsonObject;
					Message msg = new Message();

					try {
						jsonObject = new JSONObject(arg0.result);

						System.out.println(arg0.result);
						msg.obj = gson.fromJson(
								jsonObject.getString("biologies"),
								new TypeToken<ArrayList<Biology>>() {
								}.getType());

						msg.what = 8;
						handler.sendMessage(msg);

					} catch (JSONException e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();

					}
				}
			});

}
	public void getRecommendBiology() {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_rec_biology");
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 4;
						handler.sendMessage(msg);

					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 3;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("biologies"),
									new TypeToken<ArrayList<Biology>>() {
									}.getType());

							msg.what = 1;
							handler.sendMessage(msg);

						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}
					}
				});

	}

	public void getBiology(int type_id, String field_name, String keyword) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_biology");
		params.addBodyParameter("type_id", type_id + "");
		params.addBodyParameter("field_name", field_name);
		params.addBodyParameter("keyword", keyword);
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 3;
						handler.sendMessage(msg);
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 5;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("biologies"),
									new TypeToken<ArrayList<Biology>>() {
									}.getType());

							msg.what = 1;
							handler.sendMessage(msg);

						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}
					}
				});
	}
	public void check(int type_id, String field_name, String keyword) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "check_biology");
		if(type_id!=0)
		params.addBodyParameter("type_id", type_id + "");
		params.addBodyParameter("field_name", field_name);
		params.addBodyParameter("keyword", keyword);
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 3;
						handler.sendMessage(msg);
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 5;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("biologies"),
									new TypeToken<ArrayList<Biology>>() {
									}.getType());

							msg.what = 1;
							handler.sendMessage(msg);

						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}
					}
				});
	}
	public void getBiologies(int type_id, String order) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_biologies");
		params.addBodyParameter("type_id", type_id + "");
		params.addBodyParameter("order", order);
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 3;
						handler.sendMessage(msg);
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 5;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("biologies"),
									new TypeToken<ArrayList<Biology>>() {
									}.getType());
							msg.what = 1;
							handler.sendMessage(msg);

						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}

					}
				});
	}

	public void getType() {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_type");
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO 自动生成的方法存根
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 5;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO 自动生成的方法存根
						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("types"),
									new TypeToken<ArrayList<Type>>() {
									}.getType());
							msg.what = 1;
							handler.sendMessage(msg);

						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}

					}
				});
	}

	public void updateClick(int id) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "update_click");
		params.addBodyParameter("id", id + "");
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO 自动生成的方法存根
						toastInfo("网络连接错误");

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO 自动生成的方法存根

					}
				});
	}

	public void collectBiology(int type_id, int object_id, int user_id) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "collect_biology");
		params.addBodyParameter("type_id", type_id + "");
		params.addBodyParameter("object_id", object_id + "");
		params.addBodyParameter("user_id", user_id + "");
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 0;
						handler.sendMessage(msg);

					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO 自动生成的方法存根
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 2;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO 自动生成的方法存根
						if (arg0.result.equals("s")) {
							toastInfo("收藏成功");
							Message msg = new Message();
							msg.what = 3;
							handler.sendMessage(msg);
						} else {
							Message msg = new Message();
							msg.what = 4;
							handler.sendMessage(msg);
							toastInfo("收藏失败");
						}

					}
				});
	}

	public void deleteCollection(int type_id, int object_id, int user_id) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "delete_collection");
		params.addBodyParameter("type_id", type_id + "");
		params.addBodyParameter("object_id", object_id + "");
		params.addBodyParameter("user_id", user_id + "");
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 0;
						handler.sendMessage(msg);

					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO 自动生成的方法存根
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 2;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO 自动生成的方法存根

						if (arg0.result.equals("s")) {
							toastInfo("取消收藏成功");
							Message msg = new Message();
							msg.what = 5;
							handler.sendMessage(msg);
						} else {
							Message msg = new Message();
							msg.what = 6;
							handler.sendMessage(msg);
							toastInfo("取消收藏失败");
						}

					}
				});

	}

	public void getCollection(int type_id, int object_id, int user_id) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_collection");
		params.addBodyParameter("type_id", type_id + "");
		params.addBodyParameter("object_id", object_id + "");
		params.addBodyParameter("user_id", user_id + "");
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 0;
						handler.sendMessage(msg);

					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO 自动生成的方法存根
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 2;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO 自动生成的方法存根
						JSONObject jsonObject;
						Message msg = new Message();
						System.out.println(arg0.result);
						try {
							jsonObject = new JSONObject(arg0.result);
							if (jsonObject.getBoolean("iscollect")) {
								msg.arg1 = 0;
							} else {
								msg.arg1 = 1;
							}
							msg.obj = gson.fromJson(
									jsonObject.getString("collections"),
									new TypeToken<ArrayList<Collection>>() {
									}.getType());
							msg.what = 1;
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();
						}

					}
				});

	}

	public void getComment(int id,int user_id,int type_id, int object_id,String method) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_comment");
		params.addBodyParameter("id", id+"");
		params.addBodyParameter("user_id", user_id + "");
		params.addBodyParameter("type_id", type_id + "");
		params.addBodyParameter("object_id", object_id + "");
		params.addBodyParameter("method",method+"");
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 0;
						handler.sendMessage(msg);
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 2;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("comments"),
									new TypeToken<ArrayList<Comment>>() {
									}.getType());
							msg.what = 7;
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}
					}
				});
	}

	public void addFavour(int type_id, int id, int user_id) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "add_favour");
	
		params.addBodyParameter("type_id", type_id + "");
		params.addBodyParameter("id",id + "");
		params.addBodyParameter("user_id", user_id + "");
	
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 0;
						handler.sendMessage(msg);

					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO 自动生成的方法存根
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 2;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO 自动生成的方法存根
						if (arg0.result.equals("s")) {

							Message msg = new Message();
							msg.what = 5;
							handler.sendMessage(msg);
						} else {
							Message msg = new Message();
							msg.what = 4;
							handler.sendMessage(msg);

						}

					}
				});
	}
public void deleteFavour(int type_id, int id, int user_id){
	HttpUtils httpUtils = new HttpUtils(6 * 1000);
	httpUtils.configRequestRetryCount(3);
	RequestParams params = new RequestParams();
	params.addBodyParameter("type", "delete_favour");	
	params.addBodyParameter("type_id", type_id + "");
	params.addBodyParameter("object_id",id + "");
	params.addBodyParameter("user_id", user_id + "");

	httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
			+ ConstantS.server + "BiologyServlet", params,
			new RequestCallBack<String>() {

				@Override
				public void onStart() {
					// TODO 自动生成的方法存根
					Message msg = new Message();
					msg.what = 0;
					handler.sendMessage(msg);

				}

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					// TODO 自动生成的方法存根
					toastInfo("网络连接错误");
					Message msg = new Message();
					msg.what = 2;
					handler.sendMessage(msg);
				}

				@Override
				public void onSuccess(ResponseInfo<String> arg0) {
					// TODO 自动生成的方法存根
					if (arg0.result.equals("s")) {

						Message msg = new Message();
						msg.what = 7;
						handler.sendMessage(msg);
					} else {
						Message msg = new Message();
						msg.what = 4;
						handler.sendMessage(msg);

					}

				}
			});
	
	
}
	public void addComment(Comment comment) {
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "add_comment");
		params.addBodyParameter("type_id", comment.getType_id() + "");
		params.addBodyParameter("object_id", comment.getObject_id() + "");
		params.addBodyParameter("user_id", comment.getUser_id() + "");
		params.addBodyParameter("user_name", comment.getUser_name());
		params.addBodyParameter("user_agent", comment.getUser_agent());
		params.addBodyParameter("content", comment.getContent());
        params.addBodyParameter("is_reply",comment.getIs_reply()+"");
        params.addBodyParameter("reply_id",comment.getReply_id()+"");
        params.addBodyParameter("above_id",comment.getAbove_id()+"");
       
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						super.onStart();
						pd = ProgressDialog.show(context, null, "正在提交…");
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						pd.dismiss();
						toastInfo("网络连接错误");

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						pd.dismiss();
						JSONObject jsonObject;
						Message msg = new Message();
						if (arg0.result.equals("s")) {
							msg.what = 3;
							handler.sendMessage(msg);
							toastInfo("提交成功");
						} else {
							
							toastInfo("提交失败");
						}
					}
				});
	}
	public void addDynamic(Dynamic dynamic,String s){
		
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();		
		params.addBodyParameter("type", "add_dynamic");
	    params.addBodyParameter("user_id",dynamic.getUser_id()+"");
	    params.addBodyParameter("content",dynamic.getContent());
	    params.addBodyParameter("ip",dynamic.getUser_ip());
	    params.addBodyParameter("coordinate",dynamic.getUser_coordinate());
	    params.addBodyParameter("location",dynamic.getUser_location());
        params.addBodyParameter("photo",s); 
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server+"BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						pd = ProgressDialog.show(context, null, "正在提交…");
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 3;
						handler.sendMessage(msg);
						pd.dismiss();
						toastInfo("网络连接错误");
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO 自动生成的方法存根
						pd.dismiss();
						try {

							if (arg0.result.equals("s")) {
								toastInfo("发表成功");
								Message msg = new Message();
								msg.what=1;
								handler.sendMessage(msg);
								
							
							
								
							}  else if (arg0.result.equals("f")) {
							
								toastInfo("数据库查询错误");
							}
						} catch (Exception e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}
					}

				});
		
	}
	public void getDynamic(int user_id,int id,String method){
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_dynamic");
		params.addBodyParameter("user_id", user_id + "");
		params.addBodyParameter("id", id + "");
		params.addBodyParameter("method",method);
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 0;
						handler.sendMessage(msg);
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 2;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("dynamics"),
									new TypeToken<ArrayList<Dynamic>>() {
									}.getType());
							msg.what = 1;
							msg.arg1=jsonObject.getInt("count");
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}
					}
				});
	}
	public void getMyDynamic(int user_id,int id,String method){
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_my_dynamic");
		params.addBodyParameter("user_id", user_id + "");
		params.addBodyParameter("id", id + "");
		params.addBodyParameter("method",method);
		
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						Message msg = new Message();
						msg.what = 0;
						handler.sendMessage(msg);
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
						Message msg = new Message();
						msg.what = 2;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("dynamics"),
									new TypeToken<ArrayList<Dynamic>>() {
									}.getType());
							msg.what = 1;
							msg.arg1=jsonObject.getInt("count");
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}
					}
				});
	}
	public void getMyFirstDynamic(int user_id){
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_first_dynamic");
		params.addBodyParameter("user_id", user_id + "");


		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
					
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("dynamic"),
									new TypeToken<Dynamic>() {
									}.getType());
							msg.what = 3;
							msg.arg1=jsonObject.getInt("count");
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}
					}
				});
	}
	public void deleteDynamic(String id){
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "delete_dynamic");

		params.addBodyParameter("id", id);
	
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						pd = ProgressDialog.show(context, null, "正在删除…");
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
						pd.dismiss();
						
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						pd.dismiss();
						

						if (arg0.result.equals("s")) {
							toastInfo("删除成功");
							Message msg = new Message();
							msg.what=3;
							handler.sendMessage(msg);
							
						
						
							
						}  else if (arg0.result.equals("f")) {
						
							toastInfo("数据库查询错误");
						}
					}
				});
	}
	public void getNotice(int user_id){
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_notice");
		params.addBodyParameter("user_id", user_id + "");
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						//toastInfo("网络连接错误");
					
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						JSONObject jsonObject;
						Message msg = new Message();

						try {
							jsonObject = new JSONObject(arg0.result);

							System.out.println(arg0.result);
							msg.obj = gson.fromJson(
									jsonObject.getString("notices"),
									new TypeToken<ArrayList<Notice>>() {
									}.getType());
							msg.what = 3;
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();

						}
					}
				});
		
	}
	public void addNotice(Notice notice){
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "add_notice");
		params.addBodyParameter("user_id", notice.getUser_id() + "");
		params.addBodyParameter("title", notice.getTitle());
		params.addBodyParameter("content",notice.getContent());

		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
					
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						

					}
				});
		
	}
	public void updateNotice(int id){
		HttpUtils httpUtils = new HttpUtils(6 * 1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "update_notice");
		params.addBodyParameter("id", id + "");


		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// TODO 自动生成的方法存根
						
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						toastInfo("网络连接错误");
					
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub

						if (arg0.result.equals("s")) {

							Message msg = new Message();
							msg.what = 5;
							handler.sendMessage(msg);
						} else {
							Message msg = new Message();
							msg.what = 4;
							handler.sendMessage(msg);

						}

					}
				});
	}
	public void getRandomBiology(){
		HttpUtils httpUtils=new HttpUtils(6*1000);
		httpUtils.configRequestRetryCount(3);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "get_random_biology");
		
		httpUtils.send(HttpRequest.HttpMethod.POST, ConstantS.URL_IP
				+ ConstantS.server + "BiologyServlet", params, new RequestCallBack<String>() {

			@Override
			public void onStart() {
				// TODO 自动生成的方法存�?
				handler.sendEmptyMessage(ConstantS.GET_RESOURCE_START);//发回消息
			}
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO 自动生成的方法存�?
//					toastInfo("网络错误");
				handler.sendEmptyMessage(ConstantS.GET_RESOURCE_BAD);//发回消息
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO 自动生成的方法存�?
				try {
					JSONObject jsonObject=new JSONObject(arg0.result);
					System.out.println(arg0.result);
					if(jsonObject.getBoolean("status")){//服务器连接状况，
						if (jsonObject.has("biologies")) {//是否有花�?
							
							Message message=new Message();
							message.obj=gson.fromJson(jsonObject.getString("biologies"), new TypeToken<ArrayList<Biology>>(){}.getType());;
							message.what=ConstantS.GET_RESOURCE_OK;
							handler.sendMessage(message);//发回成功消息
							
						}else{
							//没有任何记录
							toastInfo("不存在树木资源");
							handler.sendEmptyMessage(ConstantS.GET_RESOURCE_BAD);//发回消息
						}
					}else{
						//服务器查询错�?
						toastInfo("服务器查询错误");
						handler.sendEmptyMessage(ConstantS.GET_RESOURCE_BAD);//发回消息
					}
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendEmptyMessage(ConstantS.GET_RESOURCE_BAD);//发回消息
				}
			}
			
		});
	}

	
}
